package com.ruoyi.web.controller.gwc;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.AppPage;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gwc.domain.TVehicleApplication;
import com.ruoyi.gwc.service.ITVehicleApplicationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TApprovalApply;
import com.ruoyi.gwc.service.ITApprovalApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 申请用车审批Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/apply")
public class TApprovalApplyController extends BaseController
{
    @Autowired
    private ITApprovalApplyService tApprovalApplyService;
    @Autowired
    private ITVehicleApplicationService tVehicleApplicationService;

    /**
     * 查询申请用车审批列表
     */
    @ApiOperation("查询申请用车审批列表")
    @GetMapping("/list")
    public TableDataInfo list(TApprovalApply tApprovalApply)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tApprovalApply.getCompanyId()))) {
            tApprovalApply.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TApprovalApply> list = tApprovalApplyService.selectTApprovalApplyList(tApprovalApply);
        return getDataTable(list);
    }

    /**
     * 导出申请用车审批列表
     */
    @Log(title = "申请用车审批", businessType = BusinessType.EXPORT)
    @ApiOperation("导出申请用车审批列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TApprovalApply tApprovalApply)
    {
        LoginUser loginUser = getLoginUser();
        Long approvealStatus = tApprovalApply.getApprovealStatus();
        List<TVehicleApplication> list = new ArrayList<>();
        //审批状态（0:通过，1：不通过，2：待审批）
        if(approvealStatus == 0 || approvealStatus == 1 ) {
            TApprovalApply approvalApply = new TApprovalApply();
            approvalApply.setApprovealStatus(approvealStatus);
            if (!loginUser.getUser().isAdmin()) {
                approvalApply.setCreateBy(loginUser.getUser().getUserName());
            }
            list = tVehicleApplicationService.getApprovalVehicleList(approvalApply);
        }else {
            TVehicleApplication vehicleApplication = new TVehicleApplication();
            vehicleApplication.setOrderStatus(0L);//订单状态（0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单）
            list = tVehicleApplicationService.selectTVehicleApplicationList(vehicleApplication);
        }

        ExcelUtil<TVehicleApplication> util = new ExcelUtil<TVehicleApplication>(TVehicleApplication.class);
        util.hideColumn("remark");
        util.exportExcel(response, list, "申请用车审批数据");
    }

    /**
     * 获取申请用车审批详细信息
     */
    @ApiOperation("获取申请用车审批详细信息")
    @GetMapping(value = "/{approvalId}")
    public AjaxResult getInfo(@PathVariable("approvalId") Long approvalId)
    {
        return success(tApprovalApplyService.selectTApprovalApplyByApprovalId(approvalId));
    }

    /**
     * 新增申请用车审批
     */
    @Log(title = "申请用车审批", businessType = BusinessType.INSERT)
    @ApiOperation("新增申请用车审批")
    @PostMapping
    public AjaxResult add(@RequestBody TApprovalApply tApprovalApply)
    {
        if( tApprovalApplyService.insertTApprovalApply(tApprovalApply) > 0){
            TVehicleApplication tVehicleApplication = new TVehicleApplication();
            tVehicleApplication.setApplyId(tApprovalApply.getApplyId());
            //审批状态（0:通过，1：不通过，2：待审批）
            if (tApprovalApply.getApprovealStatus() == 0) {
                tVehicleApplication.setOrderStatus(1L);//1：已审批
            }else if (tApprovalApply.getApprovealStatus() == 1){
                tVehicleApplication.setOrderStatus(7L);//7已驳回
            }else {

            }
            tVehicleApplicationService.updateTVehicleApplication(tVehicleApplication);
        }
        return success();
    }

    /**
     * 修改申请用车审批
     */
    @Log(title = "申请用车审批", businessType = BusinessType.UPDATE)
    @ApiOperation("修改申请用车审批")
    @PutMapping
    public AjaxResult edit(@RequestBody TApprovalApply tApprovalApply)
    {
        return toAjax(tApprovalApplyService.updateTApprovalApply(tApprovalApply));
    }

    /**
     * 删除申请用车审批
     */
    @Log(title = "申请用车审批", businessType = BusinessType.DELETE)
    @ApiOperation("删除申请用车审批")
	@DeleteMapping("/{approvalIds}")
    public AjaxResult remove(@PathVariable Long[] approvalIds)
    {
        return toAjax(tApprovalApplyService.deleteTApprovalApplyByApprovalIds(approvalIds));
    }

    @ApiOperation("查询用车审批单")
    @GetMapping("/getApprovalbyApplyId")
    public AjaxResult getApprovalbyApplyId(@RequestParam Long applyId) {
        TApprovalApply tApprovalApply = new TApprovalApply();
        tApprovalApply.setApplyId(applyId);
        List<TApprovalApply> list = tApprovalApplyService.selectTApprovalApplyList(tApprovalApply);
        return success(list);
    }

    @ApiOperation("查询审批用车审批单")
    @GetMapping("/getApprovalVehicleList")
    public TableDataInfo getApprovalVehicleList(@RequestParam Long approvealStatus,@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        startPage();
        LoginUser loginUser = getLoginUser();
        List<TVehicleApplication> list = new ArrayList<>();
        //审批状态（0:通过，1：不通过，2：待审批）
        if(approvealStatus == 0 || approvealStatus == 1 ) {
            TApprovalApply approvalApply = new TApprovalApply();
            if (!loginUser.getUser().isAdmin()) {
                approvalApply.setCreateBy(loginUser.getUser().getUserName());
                approvalApply.setCompanyId(loginUser.getUser().getDeptId());
            }
            list = tVehicleApplicationService.getApprovalVehicleList(approvalApply);
        }else {
            TVehicleApplication vehicleApplication = new TVehicleApplication();
            if (!loginUser.getUser().isAdmin()) {
                vehicleApplication.setCompanyId(loginUser.getUser().getDeptId());
            }
            vehicleApplication.setOrderStatus(0L);//订单状态（0：已申请，1：已审批，2已调度，3已接单，4已出车，5已还车，6撤销订单）
            list = tVehicleApplicationService.selectTVehicleApplicationList(vehicleApplication);
        }
        return getDataTable(list);
    }

}
